﻿using MicroCloud.Entity;
using MicroCloud.Systems;
using Microsoft.AspNetCore.Authorization;
using Microsoft.Extensions.Configuration;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Vinno.Brand.Pack.Core.Dtos;
using Vinno.Brand.Pack.Data;
using Vinno.Brand.Pack.Extensions;
using SettingInputDto = Vinno.Brand.Pack.Core.Dtos.SettingInputDto;
using SettingOutputDto = Vinno.Brand.Pack.Core.Dtos.SettingOutputDto;

namespace Vinno.Brand.Apis.Areas.Admin.Controllers
{
    /// <summary>
    /// 基础设置
    /// </summary>
    [Description("基础设置")]
    [ModuleInfo(OrderNo = 1, Position = "Brand", PositionName = "小程序模块")]
    public class SettingController: AdminControllerBase
    {
        /// <summary>
        /// 获取 字典数据存储
        /// </summary>
        protected IKeyValueStore KeyValueStore => ServiceProvider.GetRequiredService<IKeyValueStore>();

        [HttpPost]
        [UnitOfWork]
        [Description("获取小程序基础信息")]
        [ModuleInfo]
        public AjaxResult Get()
        {
            var key = SettingConstants.Setting.ToMD5();
            var value = KeyValueStore.GetByKey(key)?.Value as string;
            SettingOutputDto outPutDto = new SettingOutputDto();
            if (!value.IsNullOrWhiteSpace())
            {
                SettingDto dto = value.FromJsonString<SettingDto>();
                outPutDto = dto.MapTo<SettingOutputDto>();
            }
            return AjaxResult.Success(outPutDto);
        }

        //FileEntity
        [HttpPost]
        [UnitOfWork]
        [Description("测试小程序基础信息")]
        [ModuleInfo]
        [AllowAnonymous]
        public async Task<AjaxResult> TestSave(FileTestDto entity)
        {
            return AjaxResult.Success();
        }


        [HttpPost]
        [UnitOfWork]
        [Description("更新小程序基础信息")]
        [ModuleInfo]
        public async Task<AjaxResult> Save(SettingInputDto inputDto)
        {
            var key = SettingConstants.Setting.ToMD5();
            var dto = inputDto.MapTo<SettingDto>();
            var result = await KeyValueStore.CreateOrUpdate(key, dto.ToJsonString(), SettingConstants.Setting, "小程序基础配置信息");
            return result.ToAjaxResult();
        }

        [HttpPost]
        [ModuleInfo]
        public object ReadNode()
        {
            var provinces = ServiceProvider.GetService<IConfiguration>().GetProvincesOptions()?.Provinces;
            var datas = provinces.Select(a => new
            {
                Code = a.Key,
                Desc = a.Value
            });
            return datas;
        }

    }

    public class FileTestDto
    {
        [Required(AllowEmptyStrings = true)]
        [DisplayName("编号")]
        public virtual long Id { get; set; }

        public string FileName { get; set; }

  
        public string FileExtension { get; set; }


        public long Size { get; set; }


        public string SerializedName { get; set; }

 
        public string SHA1 { get; set; }


        public string MD5 { get; set; }

 
        public string PhysicalPath { get; set; }


        public string RequestPath { get; set; }

        public long Version { get; set; }

    }
}
